﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Model;
using YouHP.IDAL;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace YouHP.SqlServerDAL
{
    public class HyperLinkDAL : IHyperLink
    {

        #region 链接

        public bool AddHyperLink(HyperLinkInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.HyperLinkSP_AddHyperLink,
                info.IsLogo, info.Title, info.ImagePath,info.CreateTime,info.LastUpdateTime,info.Brif);
            return cnt > 0;
        }

        public bool DelHyperLink(int[] itemIds)
        {
            if (itemIds.Length == 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" [id] in ({0}", itemIds[0]);
            int cnt = itemIds.Length;
            for (int i = 1; i < cnt; i++)
            {
                sb.AppendFormat(",{0}", itemIds[i]);
            }
            sb.Append(") ");
            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.HyperLinkSP_DelHyperLink,
                sb.ToString());
            return cnt2 >= cnt;
        }

        public bool UpdateHyperLink(HyperLinkInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.HyperLinkSP_UpdateHyperLink,
                info.Id, info.IsLogo, info.Title, info.ImagePath, info.CreateTime, info.LastUpdateTime, info.Brif);
            return cnt > 0;
        }

        public HyperLinkInfo GetHyperLink(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.HyperLinkSP_GetHyperLink,
                id);
            return CBO.FillObject<HyperLinkInfo>(reader);
        }

/*
        public List<HyperLinkInfo> GetHyperLinkList()
        {
            return GetHyperLinkList("");
        }
        */
        private List<HyperLinkInfo> GetHyperLinkList(string condstr)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.HyperLinkSP_GetHyperLinkList,
                condstr);
            return CBO.FillCollection<HyperLinkInfo>(reader);

  
        }

        public List<HyperLinkInfo> GetHyperLinkList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.HyperLinkSP_GetHyperLinkList
                           ,"");

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<HyperLinkInfo>(dr);
        }
        #endregion
    }
}